package com.onepiece.gen.frame.exception;

import com.onepiece.gen.common.exception.BusiException;
import com.onepiece.gen.frame.view.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author Administrator
 * Date 2022/12/7 15:26
 * Version 1.0
 * Description TODO
 **/
@RestControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public AjaxResult runTimeExceptionHandler(RuntimeException e, HttpRequest request){
        return AjaxResult.error(String.format("系统运行异常:[%s]",e.getMessage()));
    }
    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e) {
        return AjaxResult.error(String.format("发生系统异常:",e.getMessage()));
    }

    @ExceptionHandler(BusiException.class)
    public AjaxResult handleBusiException(BusiException e) {
        return AjaxResult.error(e.getCode(),e.getMessage());
    }
}
